package com.stn.zjx.givingtreasure.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.allenliu.badgeview.BadgeFactory;
import com.allenliu.badgeview.BadgeView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.activity.web.GenericWebActivity;
import com.stn.zjx.givingtreasure.domain.MsgListItemDomain;
import com.stn.zjx.givingtreasure.domain.MsgTypeListDomain;
import com.stn.zjx.givingtreasure.utils.HtmlRegexpUtil;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxActivityTool;

import java.util.ArrayList;
import java.util.List;

public class MsgItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<MsgListItemDomain.DataBean> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;

   public MsgItemListAdapter(Context context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_msg_item_list, parent, false));
       return vh;
   }
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       MsgListItemDomain.DataBean  dataBean= dataBeanList.get(i);
             switch (dataBean.getMsgType()){
                 case 0:
                  if(TextUtils.isEmpty(dataBean.getMsgImg())){
                      ih.head_img.setVisibility(View.GONE);
                 ;     ih.left_img.setVisibility(View.GONE);
                  }else{
                      ih.left_img.setVisibility(View.GONE);
                      ih.head_img.setVisibility(View.VISIBLE);
                      Glide.with(context)
                              .load(dataBean.getMsgImg())
                              .into(ih.head_img);
                  }
                     break;
                 case 1:
                     ih.head_img.setVisibility(View.GONE);
                     ;     ih.left_img.setVisibility(View.GONE);
                     break;
                 case 2:
                     if(TextUtils.isEmpty(dataBean.getMsgImg())){
                              ih.head_img.setVisibility(View.GONE);
                         ;     ih.left_img.setVisibility(View.GONE);
                     }else{
                         ih.head_img.setVisibility(View.GONE);
                         ih.left_img.setVisibility(View.VISIBLE);
                         Glide.with(context)
                                 .load(dataBean.getMsgImg())
                                 .into(ih.left_img);
                     }
                     break;
             }
       ih.time.setText(ToolsUtils.getDateBeforeAfter(dataBean.getMsgCreatedate()));
       ih.title.setText(dataBean.getMsgTitle());
       ih.content.setText(Html.fromHtml(dataBean.getMsgContent()));
       if(!TextUtils.isEmpty(dataBean.getMsgUrl())){
           ih.rootView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   Bundle bundle=new Bundle();
                   bundle.putString("url",dataBean.getMsgUrl());
                   RxActivityTool.skipActivity(context, GenericWebActivity.class,bundle);
               }
           });
       }else{
           ih.rootView.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
               }
           });
       }

   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<MsgListItemDomain.DataBean> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<MsgListItemDomain.DataBean> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
           public TextView time;
           public ImageView head_img,left_img;
           public TextView title,content;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            time=itemView.findViewById(R.id.time);
            head_img=itemView.findViewById(R.id.head_img);
            left_img=itemView.findViewById(R.id.left_img);
            title=itemView.findViewById(R.id.title);
            content=itemView.findViewById(R.id.content);
        }
   }

}